<template>
  <div class="app-container">
    <el-row :gutter="10" v-if="optType != 'view'" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-if="buttonType != '2'">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-if="buttonType!='2'"
          v-hasPermi="['mes:pro:proroute:remove']"
        >删除</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="routeproductbomList" @selection-change="handleSelectionChange"
      max-height="620">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品物料编码" align="center" prop="itemCode" />
      <el-table-column label="产品物料名称" align="center" prop="itemName" width="200px" />
      <el-table-column label="规格型号" align="center" prop="specification" width="200px" />
      <el-table-column label="用量:分子" align="center" prop="jdyNumerator">
        <template slot-scope="scope">
          <el-input v-model="scope.row.jdyNumerator" @change="changeInput" placeholder="请输入分子" type=number
            :disabled="buttonType == '2'"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="用量:分母" width="90px" align="center" prop="jdyDenominator">
        <!-- <template slot-scope="scope">
          <span>{{
            scope.row && scope.row.jdyDenominator ? scope.row.jdyDenominator : 1
          }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <!--      <el-table-column label="用料比例" align="center" prop="quantity" />-->
      <el-table-column label="操作" align="center" v-if="optType != 'view'" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row, scope.$index)"
            v-if="buttonType != '2'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- v-if="buttonType == '2'"  -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList2" />
    <ItemBomSelectSingle ref="itemBom1" :itemId="productId" :routeId="routeId" :processId="processId"
      :processName="processName" :processCode="processCode" @backEndData="getList"></ItemBomSelectSingle>
  </div>
</template>

<script>
import {
  listRouteproductbom,
  getRouteproductbom,
  delRouteproductbom,
  addRouteproductbom,
  updateRouteproductbom,
} from "@/api/mes/pro/routeproductbom";
import ItemBomSelectSingle from "@/components/itemBomSelect/single.vue";
export default {
  name: "Routeproductbom",
  components: { ItemBomSelectSingle },
  props: {
    routeId: null,
    productId: null,
    processId: null,
    processName: null,
    processCode: null,
    workorderCode: null,
    optType: null,
    buttonType: null,
    subassemblyList: null,
    workorderId: null,
    bomItemCode: null
  },
  data() {
    return {
      // 遮罩层&
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品制程物料BOM表格数据
      routeproductbomList: [],
      allList: this.subassemblyList,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        jdyItemId: this.jdyItemId,
        pageNum: 1,
        pageSize: 10,
        routeId: this.routeId,
        workorderCode: this.workorderCode,
        processId: this.processId,
        productId: this.productId,
        workorderId: this.workorderId,
        bomItemCode: this.bomItemCode,
        // itemId: this.productId,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantity: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        routeId: [
          { required: true, message: "工艺路线不能为空", trigger: "blur" },
        ],
        processId: [
          { required: true, message: "工序不能为空", trigger: "blur" },
        ],
        productId: [
          { required: true, message: "产品物料不能为空", trigger: "blur" },
        ],
        itemId: [
          { required: true, message: "产品BOM物料不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // if (this.buttonType == "2") {
    //   this.routeproductbomList = [];
    //   this.getList2();
    // }
    // else{
    //   this.getList();
    // }
  },
  watch: {
    buttonType: function (val) {
      console.log('buttonType', buttonType)
      this.buttonType = val;
      if (val == "2") {
        this.routeproductbomList = [];
        this.getList2();
      } else {
        console.log(
          this.routeproductbomList,
          "=====this.routeproductbomList======"
        );
        // this.routeproductbomList = [];
      }
    },
    subassemblyList: function (val) {
      this.allList = val;
    },
    // kilameter: function(val) {
    //   this.meter = val * 1000;
    // }
    workorderId: function (val) {
      this.workorderId = val;
      this.queryParams.workorderId = val;
    },
    productId: {
      handler(val) {
        this.productId = val;
        this.queryParams.productId = val;
      },
    },
    processId: {
      handler(val) {
        this.queryParams.processId = val;
      },
    },
    routeId: {
      handler(val) {
        this.queryParams.routeId = val;
      },
    },
    bomItemCode: {
      handler(val) {
        this.queryParams.bomItemCode = val;
      },
    },
  },
  methods: {
    init() {
      console.log('生产确认？我被触发了')
      delete this.queryParams.productId
      this.queryParams.pageNum = 1
      this.getList2();
      // this.getList()
    },
    /** 查询产品制程物料BOM列表 */
    getList(msg) {
      if (msg != undefined) {
        let Repetitive = this.compare(msg, this.routeproductbomList);
        console.log(Repetitive, "Repetitive");
        if (Repetitive.length != 0) {
          this.$modal.msgError("产品物料编码为" + Repetitive + "已被选择");
          return;
        }
        this.routeproductbomList = this.routeproductbomList.concat(msg);
        console.log(msg, "子组件传过来 的数据");
        this.$emit("backEndData", msg, "add");
      }

      // debugger;
      //this.loading = true;
      // listRouteproductbom(this.queryParams).then(response => {
      //   console.info("response.rows", response.rows);
      //   this.routeproductbomList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    compare(arr1, arr2) {
      return arr1.filter((v) => {
        return arr2.includes(v);
      });
    },
    getList2(msg) {
      this.loading = true;
      console.log(this.queryParams, 'this.queryParams');
      listRouteproductbom(this.queryParams).then((response) => {
        console.info("response.rows====================================", response.rows);
        this.routeproductbomList = response.rows || [];
        this.total = response.total;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jdyItemId: null,
        recordId: null,
        routeId: this.routeId,
        workorderCode: this.workorderCode,
        processId: this.processId,
        productId: this.productId,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantity: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.recordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      console.log(this.allList, "需要回显的数据");
      this.$nextTick(() => {
        this.$refs.itemBom1.init(this.allList, this.routeproductbomList);
        this.$refs.itemBom1.showFlag = true;
      });
      this.title = "添加产品制程物料BOM";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids;
      getRouteproductbom(recordId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品制程物料BOM";
      });
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.recordId != null) {
    //         updateRouteproductbom(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addRouteproductbom(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      console.log(row, index);
      this.routeproductbomList.splice(index, 1);
      console.log(
        this.routeproductbomList,
        "=======this.routeproductbomList====="
      );
      // const recordIds = row.recordId || this.ids;
      // this.$modal
      //   .confirm(
      //     '是否确认删除产品制程物料BOM编号为"' + recordIds + '"的数据项？'
      //   )
      //   .then(function() {
      //     return delRouteproductbom(recordIds);
      //   })
      //   .then(() => {
      //     this.getList();
      //     this.$modal.msgSuccess("删除成功");
      //   })
      //   .catch(() => {});

      // let ids = JSON.parse(localStorage.getItem("Ids"));
      // ids = ids.filter(function(item) {
      //   return item !== row.itemCode;
      // });
      // console.log(ids, "=======ids======");
      // localStorage.setItem("Ids", JSON.stringify(ids));
      this.$emit("backEndData", row, "del");
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pro/routeproductbom/export",
        {
          ...this.queryParams,
        },
        `routeproductbom_${new Date().getTime()}.xlsx`
      );
    },
    handleItemBomSelect() {
      this.$refs.itemBom.showFlag = true;
      this.$refs.itemBom.getList();
    },
    // onItemBomSelected(row){
    //   this.form.itemId = row.itemId;
    //   this.form.itemCode = row.itemCode;
    //   this.form.itemName = row.sonname;
    //   this.form.specification = row.specification;
    //   this.form.measureName = row.measureName;
    //   this.form.unitOfMeasure = row.unitOfMeasure;
    //   this.form.quantity = row.quantity;
    // }
    // onItemBomSelected(backEndData){
    //   console.info('backEndData====>',backEndData)
    // }
    //输入分子的change
    changeInput(e) {
      console.log(e, 'e');
    },
  },
};
</script>
